{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现基于iris数据集，使用 函数，建立Sepal.Length、Sepal.Width、Petal.Length对Petal.Width的分位数回归模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.18543956,  0.125     ,  0.48076923])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.optimize import linprog\n",
    "\n",
    "iris=pd.read_csv(\"http://image.cador.cn/data/iris.csv\")\n",
    "x = iris.iloc[:,[0,1,2]]\n",
    "y = iris.iloc[:,3]\n",
    "tau = 0.35\n",
    "n,m = x.shape\n",
    "c = [0]*2*m + [tau]*n + [1-tau]*n\n",
    "A_eq = np.c_[x,-x,np.identity(n),-np.identity(n)]\n",
    "b_eq = y\n",
    "r=linprog(c,A_eq=A_eq,b_eq=b_eq,method='simplex')\n",
    "\n",
    "# 求解的回归系数\n",
    "r.x[0:3]-r.x[3:6]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
